package com.link.base.base.accnt.service;

import com.link.base.user.model.CoreUser;
import com.link.base.base.accnt.dao.mybatis.mapper.RegisterAuditMapper;
import com.link.base.base.accnt.model.Account;
import com.link.base.base.accnt.model.RegisterAudit;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * DMS注册审核-DMS注册审核
 *
 * @author 黄正杨
 * @date 2019/03/19 18:39
 */
@Service
public class RegisterAuditServiceImpl extends BasicServiceImpl<RegisterAudit> implements RegisterAuditService {

    @Resource
    private RegisterAuditMapper registerAuditMapper;
    @Resource
    private KeyGenerateService keyGenerateService;


    @Override
    public BasicMapper<RegisterAudit> getBasicMapper() {
        return registerAuditMapper;
    }

    @Override
    public void addRegisterAudit(Account entity) throws Exception {
        if (null == entity) {
            return;
        }
        // 将从前端页面接收到的信息转化为注册审核对象存入数据库
        RegisterAudit audit = new RegisterAudit();
        audit.setCity(entity.getCity());
        audit.setId(keyGenerateService.keyGenerate());
        audit.setContactName(entity.getContactName());
        audit.setDetailedAddress(entity.getAddress());
        audit.setContactPhone(entity.getMobilePhone());
        audit.setCorporateName(entity.getAcctName());
        audit.setDistrict(entity.getDistrict());
        audit.setProvince(entity.getProvince());
        audit.setState("待审核");
        audit.setCorpid(entity.getCorpid());
        registerAuditMapper.addData(audit);
    }

    @Override
    @Transactional
    public void agreeRegister(RegisterAudit audit) throws Exception {
        CoreUser user = UserUtil.getUser();
        if (null == user) {
            throw new ServiceException("PUBLIC-027");
        }
        audit = registerAuditMapper.queryById(audit);
        if (null == audit) {
            throw new ServiceException("PUBLIC-028");
        }
        if (!audit.getState().equals("待审核")) {
            throw new ServiceException("PUBLIC-029");
        }
        //修改审批状态
        audit.setLastUpdatedBy(user.getId());
        audit.setState("已通过");
        registerAuditMapper.updateState(audit);
        //因为是在注册中间穿插的逻辑，并没有修改之前的逻辑，在这里模拟之前的调用方式
        Account account = new Account();
        account.setMobilePhone(audit.getContactPhone());
        account.setProvince(audit.getProvince());
        account.setCity(audit.getCity());
        account.setDistrict(audit.getDistrict());
        account.setAcctName(audit.getCorporateName());
        account.setAddress(audit.getDetailedAddress());
        account.setContactName(audit.getContactName());
    }

    @Override
    public void refuseRegister(RegisterAudit audit) throws Exception {
        CoreUser user = UserUtil.getUser();
        if (null == user) {
            throw new ServiceException("PUBLIC-027");
        }
        audit = registerAuditMapper.queryById(audit);
        if (null == audit) {
            throw new ServiceException("PUBLIC-028");
        }
        if (!audit.getState().equals("待审核")) {
            throw new ServiceException("PUBLIC-029");
        }
        //修改审批状态
        audit.setLastUpdatedBy(user.getId());
        audit.setState("已拒绝");
        registerAuditMapper.updateState(audit);
    }
}

